package com.heima.article.listen;

import com.heima.article.service.ApArticleService;
import com.heima.commom.exception.CustException;
import com.heima.common.constants.message.PublishArticleConstants;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author by jojo
 * @Date 2022/3/5
 * @Description
 */
@Component
@Slf4j
public class PublishArticleListener {
    @Autowired
    private ApArticleService apArticleService;

    @RabbitListener(queuesToDeclare = @Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE))
    public void publishArticle(String newsId){
        log.info("接收到发布文章的通知 文章id：{},当前时间：{}",newsId, LocalDateTime.now());
        if (newsId==null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        try {
            //执行发布文章的方法
            apArticleService.publishArticle(Integer.valueOf(newsId));
            log.info("文章发布成功！！！");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.info("文章发布失败！！！原因是：{}",e.getMessage());
        }
    }
}
